package com.bw.ssm.controller;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author Lenovo
 */
//精确匹配
//@WebServlet(name="index3",urlPatterns="/abc/index3")
//扩展名匹配
//@WebServlet(name="index3",urlPatterns="*.kzm")
//路径匹配
//@WebServlet(name="index3",urlPatterns="/abc/*")
//匹配任意的url
@WebServlet(name="index3",urlPatterns="/*")
public class ServletUrlPattern extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * getContextPath 取得项目名 
			getServletPath 取得Servlet名  (匹配方式不一样取值不一样)
			getPathInfo 取得Servlet后的URL名，不包括URL参数 (匹配方式不一样取值不一样)
			getRequestURL 取得不包括参数的URL 
			getRequestURI 取得不包括参数的URI，即去掉协议和服务器名的URL 
		 */
		System.out.println("getContextPath："+ request.getContextPath());
		System.out.println("getServletPath："+ request.getServletPath());
		System.out.println("getPathInfo："+ request.getPathInfo());
		System.out.println("getRequestURL："+ request.getRequestURL());
		System.out.println("getRequestURI："+ request.getRequestURI());

		response.setHeader("Content-type", "text/html; charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		Writer w = response.getWriter();
		StringBuffer sb = new StringBuffer();
		sb.append("取得项目名：<br/>");
		sb.append("getContextPath："+ request.getContextPath()+"<br/><br/>");
		
		sb.append("取得Servlet名 (匹配方式不一样取值不一样)：<br/>");
		sb.append("getServletPath："+ request.getServletPath()+"<br/><br/>");
		
		sb.append("取得Servlet后的URL名，不包括URL参数 (匹配方式不一样取值不一样)：<br/>");
		sb.append("getPathInfo："+ request.getPathInfo()+"<br/><br/>");
		
		sb.append("取得不包括参数的URL：<br/>");
		sb.append("getRequestURL："+ request.getRequestURL()+"<br/><br/>");
		
		sb.append("取得不包括参数的URI，即去掉协议和服务器名的URL：<br/>");
		sb.append("getRequestURI："+ request.getRequestURI()+"<br/><br/>");
		w.write(sb.toString());
		w.flush();
	}
}
